// pages/announcement/announcement.js
import request from "../../utils/request.js";
import themeManager from "../../utils/theme.js";

Page(themeManager.enhancePage({
  data: {
    announcement: {},
    loading: false,
    currentTheme: 'dark'
  },

  onLoad: function (options) {
    // 获取从上一页传递的参数
    console.log('页面加载，接收到的参数:', options)
    this.loadAnnouncementData(options.announcementId)
  },

  // 加载公告数据
  loadAnnouncementData: function (announcementId) {
    this.setData({ loading: true });
    
    console.log('加载公告数据:', announcementId)
    request
      .get("/announcements/" + announcementId)
      .then((res) => {
        if (res) {
          this.setData({
            announcement: res.data,
            loading: false
          });
        }
      })
      .catch((err) => {
        console.error("加载公告详情失败:", err);
        this.showError("加载公告详情失败，请稍后重试");
        this.setData({ loading: false });
        // this.loadMockData() // 加载模拟数据
      });
  },

  onViewHistory: function() {
    wx.navigateTo({
      url: '/pages/announcements/announcement'
    });
  },

  // 显示错误信息
  showError: function(message) {
    wx.showToast({
      title: message,
      icon: 'none',
      duration: 2000
    });
  }
}))
